package com.fxd.rocketmq.controller;

import com.fxd.rocketmq.enums.RocketMqConfigEnums;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xudongfu
 * @date 2023/02/22
 */
@RestController()
@RequestMapping("/easy")
@Slf4j
public class EasyController {

    @Autowired
    private DefaultMQProducer defaultMQProducer;

    @GetMapping("/test/{msg}")
    public void send(@PathVariable("msg") String msg) {

        Message message = new Message();
        message.setTopic(RocketMqConfigEnums.TEST.getTopic());
        message.setTags(RocketMqConfigEnums.TEST.getTag());

        message.setBody(msg.getBytes());
        try {
            defaultMQProducer.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    log.info(sendResult.toString());
                }

                @Override
                public void onException(Throwable throwable) {
                    log.info(throwable.getMessage());
                }
            });
        } catch (Exception e) {
            log.info("发送消息发送异常: {}", e);
        }
    }
}
